package com.li.configserve.config.redis;

import com.li.configserve.domain.properties.RedisProperties;
import com.li.toolkit.constant.LogConst;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2021/2/27
 * @Version：1.0.0
 */
@Configuration
public class RedissionConfig {

    private final Logger logger = LoggerFactory.getLogger(RedissionConfig.class);

    @Bean(name = "singleClient")
    public RedissonClient singleRedissonClient(RedisProperties redisProperties) {
        try {
            Config config = new Config();
            config.useSingleServer().setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort()).setDatabase(redisProperties.getDb());
            // 设置序列化方式
            Codec codec = new JsonJacksonCodec();
            config.setCodec(codec);
            RedissonClient redissonClient = Redisson.create(config);
            redissonClient.getBucket("one").set("one");
            logger.info(LogConst.LOG_SUCCESS_PREFIX + "singleRedissonClient()配置完成！");
            return redissonClient;
        } catch (Exception e) {
            String errorMessage = LogConst.LOG_FAILURE_PREFIX + "singleRedissonClient() 出错：" + e.getMessage();
            logger.error(errorMessage);
            return null;
        }
    }

    /**
     * 主从模式
     *
     * @Bean(name = "masterSlaveClient")
     * @Primary
     */
    @SuppressWarnings("ALL")
    public RedissonClient masterSlaveClient() {
        try {
            Config config = new Config();
            config.useMasterSlaveServers().setMasterAddress("redis://127.0.0.1:6379")
                    .addSlaveAddress("redis://127.0.0.1:6380");
            RedissonClient redissonClient = Redisson.create(config);
            redissonClient.getBucket("masterSlaveClient").set("masterSlaveClient");
            logger.info("==========> masterSlaveClient()配置完成！");
            return redissonClient;
        } catch (Exception e) {
            logger.error("==========> masterSlaveClient() 出错：{}", e);
            return null;
        }
    }


    /**
     * 哨兵模式
     *
     * @Bean(name = "sentryClient")
     */
    @SuppressWarnings("ALL")
    public RedissonClient sentryClient() {
        try {
            Config config = new Config();
            // 需要启动/redis-sentinel
            config.useSentinelServers().setMasterName("mymaster")
                    .addSentinelAddress("redis://127.0.0.1:6379")
                    .addSentinelAddress("redis://127.0.0.1:6380");
            RedissonClient redissonClient = Redisson.create(config);
            redissonClient.getBucket("sentryClient").set("sentryClient");
            logger.info("==========> sentryClient()配置完成！");
            return redissonClient;
        } catch (Exception e) {
            logger.error("==========> sentryClient() 出错：{}", e);
        }
        return null;
    }


    /**
     * 集群模式
     * Bean(name = "clustersClient")
     */
    @SuppressWarnings("ALL")
    public RedissonClient clustersClient() {
        try {
            Config config = new Config();
            config.useClusterServers()
                    .setScanInterval(2000)
                    .addNodeAddress("redis://127.0.0.1:6379")
                    .addNodeAddress("redis://127.0.0.1:6382")
                    .addNodeAddress("redis://127.0.0.1:6381");
            RedissonClient redissonClient = Redisson.create(config);
            redissonClient.getBucket("clustersClient").set("clustersClient");
            logger.info("==========> clustersClient()配置完成！");
            return redissonClient;
        } catch (Exception e) {
            logger.error("==========> clustersClient 出错：{}", e);
        }
        return null;
    }


}
